package com.baiwang.bwbaselib;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

/**
 *---------------------------------------------------
 * Description: 封装了toolbar的base Activity
 * Author: ljgsonx
 * Belong to: com.baiwang.bwbaselib
 * Time: 2016/1/5 18:48
 *---------------------------------------------------
 */
public class BaseActivity extends AppCompatActivity {

    private ToolBarHelper mToolBarHelper;
    private Toolbar mToolbar;
    private TextView mToolbarTV;
    private int mAppMode = 0;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    //toolbar title的color定义与初始化
    private int initTitleColor,finalTitleColor;
    private int delaR, delaG, delaB;

    private int mLayoutResID;

    AppBarLayout.OnOffsetChangedListener myOnOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {

        final float tvMaxSizeScale = 1.5f;
        int mTvMaxOffset;

        float lastScaleOffset = 1f, lastTranslateOffset = 0;
        int lastTitleColor = finalTitleColor;

        Animation translateAnimation,sizeAnimation;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            View homeButton = mToolbar.getChildAt(1);
            mTvMaxOffset = mToolbarTV.getLeft() - (homeButton.getLeft()+homeButton.getWidth());
            int maxScrollRange = appBarLayout.getTotalScrollRange();
            float scrollRate = ((float)verticalOffset)/maxScrollRange;
            startTitleTvAnimation(scrollRate);
            int scrollTitleColor = Color.rgb((int)(Color.red(lastTitleColor)+delaR*scrollRate),
                    (int)(Color.green(lastTitleColor)+delaG*scrollRate),
                    (int)(Color.blue(lastTitleColor)+delaB*scrollRate));
            mToolbarTV.setTextColor(scrollTitleColor);
            if(scrollRate == 1) {
                lastTitleColor = initTitleColor;
            }
            if(scrollRate == 0) {
                lastTitleColor = finalTitleColor;
            }
        }

        /**
         * Description: 开始title的尺寸及位移动画
         *             <p>其中尺寸公式:当前倍数=1+(n-1)*m m:当前倍数比例 n:最大放大倍数</p>
         * Author: ljgsonx
         * Time: 2016/1/14 1:51
         */
        private void startTitleTvAnimation(float scrollRate){
            float translateOffset = mTvMaxOffset*scrollRate;
            float scaleOffset = 1+(tvMaxSizeScale-1)*Math.abs(1+scrollRate);
            translateAnimation = new TranslateAnimation(lastTranslateOffset,translateOffset, 0, 0);
            sizeAnimation = new ScaleAnimation(lastScaleOffset,scaleOffset,lastScaleOffset,scaleOffset,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f);
            AnimationSet animationSet = new AnimationSet(true);
            animationSet.addAnimation(translateAnimation);
            animationSet.addAnimation(sizeAnimation);
            animationSet.setFillAfter(true);
            mToolbarTV.startAnimation(animationSet);
            lastTranslateOffset = translateOffset;
            lastScaleOffset = scaleOffset;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        this.mLayoutResID = layoutResID;
        initAll();
    }

    private void initAll(){
        if(null == mToolBarHelper)
            mToolBarHelper = new ToolBarHelper(this,mLayoutResID);
        mToolBarHelper.setCollapsingToolbar(mAppMode);

        setContentView(mToolBarHelper.getContentView());

        if(mAppMode == ToolBarHelper.COLLAPSING_DRAWER_MODE){
            mToolBarHelper.getAppBarLayout().addOnOffsetChangedListener(myOnOffsetChangedListener);
        }

        /*侧边栏开关*/
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        if(null != mDrawerLayout){
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.company_name,R.string.company_name);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }

        /*开始自定义toolbar*/
        onCreateCustomToolBar();

        //沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    /**
     * Description: 设置toolbar是否可伸展
     * Author: ljgsonx
     * Time: 2016/1/5 18:41
     */
    public void setCollapsingToolbar(int appMode){
        this.mAppMode = appMode;
        initAll();
    }

    /**
     * Description: 自定义toolbar
     * Author: ljgsonx
     * Time: 2016/1/5 18:41
     */
    public void onCreateCustomToolBar(){
        mToolbar = mToolBarHelper.getToolBar();
        mToolbarTV = (TextView) mToolbar.findViewById(R.id.toolbarTV);
        mToolbar.setTitle("");
        //mToolbar.setLogo(R.drawable.logo);
        //mToolbar.setContentInsetsRelative(50, 0);
        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(mToolbar);

        //必须在setSupportActionBar之后设置,也可以在toolbar xml中设置app:navigationIcon,同时自定义图标
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //初始化toolbar title的颜色
        initTitleColor = getResources().getColor(R.color.c_light_white);
        finalTitleColor = getResources().getColor(R.color.c_light_gray);
        delaR = Color.red(finalTitleColor) - Color.red(initTitleColor);
        delaG = Color.green(finalTitleColor) - Color.green(initTitleColor);
        delaB = Color.blue(finalTitleColor) - Color.blue(initTitleColor);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return (mDrawerToggle !=null && mDrawerToggle.onOptionsItemSelected(item)) || super.onOptionsItemSelected(item);
    }

    //不设时,左上角图标是个箭头
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if(null != mDrawerToggle)
            mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(null != mDrawerToggle)
            mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mToolBarHelper.getAppBarLayout().removeOnOffsetChangedListener(myOnOffsetChangedListener);
    }
}
